package com.gewu.teday.adapter

import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.gewu.teday.R


class LineChartLegendAdapet  constructor(var list: List<LegendBean>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val inflate = LayoutInflater.from(parent.context).inflate(R.layout.chart_legend_item, parent, false)
        return LegendHoder(inflate)
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val hoder = holder as LegendHoder
        if (position==0){
            hoder.image?.visibility = View.GONE
            hoder.lines?.visibility = View.VISIBLE
            hoder.core?.let { setColor(50f,list[position].Color, it) }
            hoder.color_core_core?.let { setColor(50f,"#FFFFFF", it) }
        }else{
            hoder.image?.visibility = View.VISIBLE
            hoder.lines?.visibility = View.GONE
            hoder.image?.let { setColor(5f,list[position].Color, it) }
        }
        hoder.text?.text=list[position].Text
    }


    fun setColor(radio:Float,color: String,view: View){
        val drawable = GradientDrawable()
        drawable.cornerRadius = radio
        drawable.setColor(Color.parseColor(color))
        drawable.setStroke(0, -0x1000000)
        view.background = drawable
    }

    override fun getItemCount(): Int = list.size

    class LegendHoder(inflate: View?) : RecyclerView.ViewHolder(inflate!!) {
        var image: View? = inflate?.findViewById<View>(R.id.color_view)
        var core: View? = inflate?.findViewById<View>(R.id.color_core)
        var color_core_core: View? = inflate?.findViewById<View>(R.id.color_core_core)
        var text: TextView? = inflate?.findViewById<TextView>(R.id.text_view)
        var lines: RelativeLayout? = inflate?.findViewById<RelativeLayout>(R.id.lines)
    }
}

data class LegendBean(var Color: String, var Text: String)
